package com.wisdom.admin.controller;

import com.wisdom.admin.common.api.CommonResult;
import com.wisdom.admin.dto.WxLoginParam;
import com.wisdom.admin.service.UmsAdminService;
import com.wisdom.admin.service.WxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ming
 * @ClassName WxApiController.java
 * @Description 微信相关接口
 * @createTime 2021年04月25日 17:07:00
 */
@RequestMapping(value = "/wxapi")
@Api(tags = "WxApiController",description = "微信相关接口")
@Controller
public class WxApiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WxApiController.class);

    @Autowired
    private WxService wxService;

    @Autowired
    private UmsAdminService umsAdminService;

    @ApiOperation("小程序登录")
    @RequestMapping(value = "/wxlogin",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult wxLogin(@RequestBody WxLoginParam wxLoginParam, BindingResult result){
        LOGGER.info("小程序授权信息:{}",wxLoginParam);
        Map code2Session = wxService.code2Session(wxLoginParam.getCode());
        if (code2Session == null){
            return CommonResult.failed("登录异常，code无效");
        }
        wxLoginParam.setOpenid(code2Session.get(new String("openid")).toString());
        Map<String,String> map = umsAdminService.wxlogin(wxLoginParam);
        if (map == null){
            return CommonResult.validateFailed("用户授权登录失败");
        }
        return CommonResult.success(map);
    }

    @ApiOperation("小程序获取当前用户的数据")
    @RequestMapping(value = "/getUserData",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getUserData(Principal principal){
        if (principal == null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        Map<String,String> map = new HashMap<>();
        map.put("child_all_count","5");//全部成员
        map.put("child_child_user_count","10");//直属成员
        map.put("child_user_count","20");//非直属成员
        map.put("cps_profit_sum","100.00");//等待结算佣金
        map.put("total_money","1000.00");//累计提现
        map.put("money","2000.00");//余额
        return CommonResult.success(map);
    }

    @ApiOperation("小程序获取csp配置")
    @RequestMapping(value = "/config",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult config() {
        Map<String,Object> map = new HashMap<>();
        map.put("cpslist", new ArrayList<>());
        map.put("slider",new ArrayList<>());
        return CommonResult.success(map);
    }

    @ApiOperation("更具类型获取活动")
    @RequestMapping(value = "/getTypeActivity",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getTypeActivity(){
        List<Object> lists = new ArrayList<>();
        return CommonResult.success(lists);
    }
}
